package com.xhga.transaction.common.jdbc;


import com.xhga.transaction.common.entity.TransactionLog;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by HuaWeiBo on 2019/5/5.
 */
public class JdbcTransactionLog {

    public static int addTransactionLog(TransactionLog transactionLog) {
        Connection conn = JdbcFactory.getConn();
        String sql = "insert into transaction_log(centre_no, `count`, prepare_count, failed_count)" +
                     " values(?, ?, ?, ?)";
        PreparedStatement ps;
        int rs = 0;
        try {
            ps = (PreparedStatement) conn.prepareStatement(sql);
            ps.setString(1, transactionLog.getCentreNo());
            ps.setInt(2, transactionLog.getCount());
            ps.setInt(3, transactionLog.getPrepareCount());
            ps.setInt(4, transactionLog.getFailedCount());
            rs = ps.executeUpdate();
            ps.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    public static int updateFailedCount(int id, int prepareCount, int failedCount) {
        Connection conn = JdbcFactory.getConn();
        String sql = "update transaction_log set prepare_count = ?, failed_count = ? " +
                     " WHERE id = ?";
        PreparedStatement ps;
        int rs = 0;
        try {
            ps = (PreparedStatement) conn.prepareStatement(sql);
            ps.setInt(1, prepareCount);
            ps.setInt(2, failedCount);
            ps.setInt(3, id);
            rs = ps.executeUpdate();
            ps.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    public static int updatePrepareCount(String centreNo) {
        Connection conn = JdbcFactory.getConn();
        String sql = "update transaction_log set prepare_count = prepare_count - 1 " +
                     " where centre_no = ?";
        PreparedStatement ps;
        int rs = 0;
        try {
            ps = (PreparedStatement) conn.prepareStatement(sql);
            ps.setString(1, centreNo);
            rs = ps.executeUpdate();
            ps.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    public static TransactionLog getTransactionLogByCentreNo(String centreNo){
        Connection conn = JdbcFactory.getConn();
        String sql = "select * from transaction_log  where centre_no = ? limit 1";
        PreparedStatement ps;
        ResultSet rs;
        TransactionLog transactionLog = null;
        try {
            ps = (PreparedStatement) conn.prepareStatement(sql);
            ps.setString(1, centreNo);
            rs = ps.executeQuery();
            while (rs.next()) {
                transactionLog = new TransactionLog();
                transactionLog.setId(rs.getInt(1));
                transactionLog.setCentreNo(rs.getString(2));
                transactionLog.setCount(rs.getInt(3));
                transactionLog.setPrepareCount(rs.getInt(4));
                transactionLog.setFailedCount(rs.getInt(5));
            }
            ps.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return transactionLog;
    }
}
